package com.yanqu.road.dao.impl.player;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.musicroom.UserMusicRoomBeautyData;
import com.yanqu.road.entity.musicroom.UserMusicRoomBuildingData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class UserMusicRoomBeautyDaoImpl extends TempDataObjectDao<UserMusicRoomBeautyData> {
    public static String getUserMusicRoomBeautyDataSql = "select * from `t_u_music_room_beauty` where `userId`=?";

    public Map<Integer, UserMusicRoomBeautyData> getUserMusicRoomBeautyData(long userId) {
        Map<Integer, UserMusicRoomBeautyData> dataMap = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getUserMusicRoomBeautyDataSql);
            int i = 1;
            call.setLong(i++, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                UserMusicRoomBeautyData data = parse(rs);
                dataMap.put(data.getBeautyId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    public static String updateUserMusicRoomBeautyDataSql = "update `t_u_music_room_beauty` set level=?,exp=?,comfort=?,mood=?,satiety=?,totalComfort=?,totalMood=?,totalSatiety=?,attr1=?,attr2=?,attr3=?,attr4=?,attr5=?,nowSkin=?,unlockSkins=?,liveInTime=?,liveInBuildId=?,lastTimerEndTime=?,scheduleListType=?,scheduleList=?,modifyTime=?,nowBuildingId=?,nextBuildingId=? where userId=? and beautyId=?";

    @Override
    protected boolean updateData(UserMusicRoomBeautyData userMusicRoomBeautyData) {
        userMusicRoomBeautyData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, userMusicRoomBeautyData.getLevel()));
        para.put(i++, new DbParameter(Types.BIGINT, userMusicRoomBeautyData.getExp()));
        para.put(i++, new DbParameter(Types.BIGINT, userMusicRoomBeautyData.getComfort()));
        para.put(i++, new DbParameter(Types.BIGINT, userMusicRoomBeautyData.getMood()));
        para.put(i++, new DbParameter(Types.BIGINT, userMusicRoomBeautyData.getSatiety()));
        para.put(i++, new DbParameter(Types.BIGINT, userMusicRoomBeautyData.getTotalComfort()));
        para.put(i++, new DbParameter(Types.BIGINT, userMusicRoomBeautyData.getTotalMood()));
        para.put(i++, new DbParameter(Types.BIGINT, userMusicRoomBeautyData.getTotalSatiety()));
        para.put(i++, new DbParameter(Types.BIGINT, userMusicRoomBeautyData.getAttr1()));
        para.put(i++, new DbParameter(Types.BIGINT, userMusicRoomBeautyData.getAttr2()));
        para.put(i++, new DbParameter(Types.BIGINT, userMusicRoomBeautyData.getAttr3()));
        para.put(i++, new DbParameter(Types.BIGINT, userMusicRoomBeautyData.getAttr4()));
        para.put(i++, new DbParameter(Types.BIGINT, userMusicRoomBeautyData.getAttr5()));
        para.put(i++, new DbParameter(Types.INTEGER, userMusicRoomBeautyData.getNowSkin()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userMusicRoomBeautyData.getUnlockSkins())));
        para.put(i++, new DbParameter(Types.BIGINT, userMusicRoomBeautyData.getLiveInTime()));
        para.put(i++, new DbParameter(Types.INTEGER, userMusicRoomBeautyData.getLiveInBuildId()));
        para.put(i++, new DbParameter(Types.BIGINT, userMusicRoomBeautyData.getLastTimerEndTime()));
        para.put(i++, new DbParameter(Types.INTEGER, userMusicRoomBeautyData.getScheduleListType()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userMusicRoomBeautyData.getScheduleList())));
        para.put(i++, new DbParameter(Types.BIGINT, userMusicRoomBeautyData.getModifyTime()));
        para.put(i++, new DbParameter(Types.INTEGER, userMusicRoomBeautyData.getNowBuildingId()));
        para.put(i++, new DbParameter(Types.INTEGER, userMusicRoomBeautyData.getNextBuildingId()));
        para.put(i++, new DbParameter(Types.BIGINT, userMusicRoomBeautyData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, userMusicRoomBeautyData.getBeautyId()));
        if (getDb().executeNoneQuery(updateUserMusicRoomBeautyDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertUserMusicRoomBeautyDataSql = "insert into `t_u_music_room_beauty`(userId,beautyId,level,exp,comfort,mood,satiety,totalComfort,totalMood,totalSatiety,attr1,attr2,attr3,attr4,attr5,nowSkin,unlockSkins,liveInTime,liveInBuildId,lastTimerEndTime,scheduleListType,scheduleList,modifyTime,nowBuildingId,nextBuildingId) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";

    @Override
    protected boolean addData(UserMusicRoomBeautyData userMusicRoomBeautyData) {
        userMusicRoomBeautyData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, userMusicRoomBeautyData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, userMusicRoomBeautyData.getBeautyId()));
        para.put(i++, new DbParameter(Types.INTEGER, userMusicRoomBeautyData.getLevel()));
        para.put(i++, new DbParameter(Types.BIGINT, userMusicRoomBeautyData.getExp()));
        para.put(i++, new DbParameter(Types.BIGINT, userMusicRoomBeautyData.getComfort()));
        para.put(i++, new DbParameter(Types.BIGINT, userMusicRoomBeautyData.getMood()));
        para.put(i++, new DbParameter(Types.BIGINT, userMusicRoomBeautyData.getSatiety()));
        para.put(i++, new DbParameter(Types.BIGINT, userMusicRoomBeautyData.getTotalComfort()));
        para.put(i++, new DbParameter(Types.BIGINT, userMusicRoomBeautyData.getTotalMood()));
        para.put(i++, new DbParameter(Types.BIGINT, userMusicRoomBeautyData.getTotalSatiety()));
        para.put(i++, new DbParameter(Types.BIGINT, userMusicRoomBeautyData.getAttr1()));
        para.put(i++, new DbParameter(Types.BIGINT, userMusicRoomBeautyData.getAttr2()));
        para.put(i++, new DbParameter(Types.BIGINT, userMusicRoomBeautyData.getAttr3()));
        para.put(i++, new DbParameter(Types.BIGINT, userMusicRoomBeautyData.getAttr4()));
        para.put(i++, new DbParameter(Types.BIGINT, userMusicRoomBeautyData.getAttr5()));
        para.put(i++, new DbParameter(Types.INTEGER, userMusicRoomBeautyData.getNowSkin()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userMusicRoomBeautyData.getUnlockSkins())));
        para.put(i++, new DbParameter(Types.BIGINT, userMusicRoomBeautyData.getLiveInTime()));
        para.put(i++, new DbParameter(Types.INTEGER, userMusicRoomBeautyData.getLiveInBuildId()));
        para.put(i++, new DbParameter(Types.BIGINT, userMusicRoomBeautyData.getLastTimerEndTime()));
        para.put(i++, new DbParameter(Types.INTEGER, userMusicRoomBeautyData.getScheduleListType()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userMusicRoomBeautyData.getScheduleList())));
        para.put(i++, new DbParameter(Types.BIGINT, userMusicRoomBeautyData.getModifyTime()));
        para.put(i++, new DbParameter(Types.INTEGER, userMusicRoomBeautyData.getNowBuildingId()));
        para.put(i++, new DbParameter(Types.INTEGER, userMusicRoomBeautyData.getNextBuildingId()));
        if (getDb().executeNoneQuery(insertUserMusicRoomBeautyDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected UserMusicRoomBeautyData parseData(ResultSet rs) throws SQLException {
        UserMusicRoomBeautyData userMusicRoomBeautyData = new UserMusicRoomBeautyData();
        userMusicRoomBeautyData.setUserId(rs.getLong("userId"));
        userMusicRoomBeautyData.setBeautyId(rs.getInt("beautyId"));
        userMusicRoomBeautyData.setLevel(rs.getInt("level"));
        userMusicRoomBeautyData.setExp(rs.getLong("exp"));
        userMusicRoomBeautyData.setComfort(rs.getLong("comfort"));
        userMusicRoomBeautyData.setMood(rs.getLong("mood"));
        userMusicRoomBeautyData.setSatiety(rs.getLong("satiety"));
        userMusicRoomBeautyData.setTotalComfort(rs.getLong("totalComfort"));
        userMusicRoomBeautyData.setTotalMood(rs.getLong("totalMood"));
        userMusicRoomBeautyData.setTotalSatiety(rs.getLong("totalSatiety"));
        userMusicRoomBeautyData.setAttr1(rs.getLong("attr1"));
        userMusicRoomBeautyData.setAttr2(rs.getLong("attr2"));
        userMusicRoomBeautyData.setAttr3(rs.getLong("attr3"));
        userMusicRoomBeautyData.setAttr4(rs.getLong("attr4"));
        userMusicRoomBeautyData.setAttr5(rs.getLong("attr5"));
        userMusicRoomBeautyData.setNowSkin(rs.getInt("nowSkin"));
        userMusicRoomBeautyData.setUnlockSkins(JSON.parseObject(rs.getString("unlockSkins"), new TypeReference<List<Integer>>() {
        }));
        userMusicRoomBeautyData.setLiveInTime(rs.getLong("liveInTime"));
        userMusicRoomBeautyData.setLiveInBuildId(rs.getInt("liveInBuildId"));
        userMusicRoomBeautyData.setLastTimerEndTime(rs.getLong("lastTimerEndTime"));
        userMusicRoomBeautyData.setScheduleListType(rs.getInt("scheduleListType"));
        userMusicRoomBeautyData.setScheduleList(JSON.parseObject(rs.getString("scheduleList"), new TypeReference<java.util.List<java.lang.Integer>>() {
        }));
        userMusicRoomBeautyData.setModifyTime(rs.getLong("modifyTime"));
        userMusicRoomBeautyData.setNowBuildingId(rs.getInt("nowBuildingId"));
        userMusicRoomBeautyData.setNextBuildingId(rs.getInt("nextBuildingId"));

        return userMusicRoomBeautyData;
    }
}
